package com.stroage.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stroage.domain.TStorage;
import com.stroage.mapper.TStorageMapper;
import com.stroage.service.impl.TStorageService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @author cyt
 * @create 2020-06-07 10:34
 */
@Service
public class TStorageServiceImpl extends ServiceImpl<TStorageMapper, TStorage> implements TStorageService {
    /**
     * 修改库存
     * @param productId
     * @param count
     */
    @Override
    public void dec(Integer productId, Integer count) {
        QueryWrapper<TStorage> wrapper = new QueryWrapper<TStorage>();
        wrapper.eq("product_id", productId);
        TStorage storage = baseMapper.selectOne(wrapper);
        // 更新 使用量
        int used = storage.getUsed() + count;
        // 减少库存
        int storageNum = storage.getResidue() - count;
        storage.setUsed(used);
        storage.setResidue(storageNum);
        baseMapper.update(storage, wrapper);
    }
}
